<?php

namespace App\Models\hwtc;

use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;

class Apiuser extends Authenticatable implements JWTSubject
{
    use Notifiable;
    protected $table = 'api_users';
    protected $guarded = [];
//    public $timestamps = false;

    //每个文件都有对应的文件类型
    public function customer()
    {
        return $this->belongsTo('App\Models\hwtc\Customer','customer_id');
    }
    
    public function apiroles()
    {
    	//多对多 (一个api用户有多个角色)
	    return $this->belongsToMany('App\Models\hwtc\Apirole','api_role_user','api_user_id','api_role_id');
    }


    static function ge_apiuser_roles($id){
        //控制器中直接用with('apiroles')方法实现！！！
    }

    static function get_apiuser_permissions($current_user){
        $permissions=[]; //记录当前用户所有权限
        foreach($current_user->apiroles as $role){
            $apirole_permissions= Apirole::with('apipermissions')->find($role->id);  //找出当前角色和对应的权限列表

            $permissions=array_merge($permissions,$apirole_permissions->apipermissions->toArray());

        }
        return $permissions;
    }

    ########################################

    public function getJWTIdentifier()
    {
        return $this->getKey();
    }

    public function getJWTCustomClaims()
    {
        return [];
    }
}
